Chapter 1 - Modelling & programming environments

A mathematical model is a description of a system which uses abstractions and mathematical language. The development of of one of these is know as mathematical modelling. The mathematical models usally are made of relations and variables, such relations can be described by operators (algebraical and/or differential), functions, etc. The variables are abstractions of the studied system's parameters which can be quantified.

Homework 01

First approach - Deal with data and model implementations

Nowdays in almost every field of the human knowledge is necessary to deal with data. Use a programming language becomes an essential tool when we want to automatize the implementation of our models.

1) implement a function to read data from a directory taking in count the file extention.


In [2]:
import os
import numpy as np 
path = "/data/"

def read_dir(path, ext):
    l = []
    for f in os.listdir(os.getcwd()+path):
        if f.endswith(ext):
            r = open(os.getcwd()+path+f).read()
            r = np.array(r[:-1].split())
            l.append({f:r})
    return l

2) Use the funtion to load the data on memory


In [9]:
input_data = read_dir(path,'.in') 
ans_data = read_dir(path,'.ans')

3) Create a set of functions that implements a model to treat our data.


In [10]:
def price(l):
    p = int(l[0])
    a = int(l[1])
    b = int(l[2])
    c = int(l[3])
    d = int(l[4])
    n = int(l[5])
    P = []
    for k in range(int(n)):
        P.append(p*(np.sin(a*(k+1)+b)+np.cos(c*(k+1)+d)+2))
    return P

In [11]:
def max_decline(prices):
    max_dif = 0.0
    dif = max(prices)-min(prices)
    if dif >  0.0:
        max_dif = dif
    return max_dif

In [12]:
def max_dec_list(data):
    declines = []
    for d in data:
        key = d.keys()[0]
        value = d.values()[0]
        stock_prices = price(value)
        declines.append({key:[max_decline(stock_prices)]})
    return declines
comp_data = max_dec_list(input_data)

4) Compute the error between the computed data and the teorical.


In [19]:
for l1 in ans_data:
    for l2 in comp_data:
        if l1.keys()[0][0:2] == l2.keys()[0][0:2]:
            print l1.keys()[0], l2.keys()[0]
            print 'error in file', l1.keys()[0][0:2], 'is ', '%.6f' % abs(float(l1.get(l1.keys()[0])[0])- float(l2.get(l2.keys()[0])[0]))


13.ans 13.in
error in file 13 is  0.008911
03.ans 03.in
error in file 03 is  0.000000
04.ans 04.in
error in file 04 is  0.000000
05.ans 05.in
error in file 05 is  0.000000
06.ans 06.in
error in file 06 is  76.507643
07.ans 07.in
error in file 07 is  0.015731
08.ans 08.in
error in file 08 is  0.004480
09.ans 09.in
error in file 09 is  0.000892
10.ans 10.in
error in file 10 is  0.000000
11.ans 11.in
error in file 11 is  0.000000
12.ans 12.in
error in file 12 is  0.000000
14.ans 14.in
error in file 14 is  0.000000
15.ans 15.in
error in file 15 is  0.000000
16.ans 16.in
error in file 16 is  63.880478
17.ans 17.in
error in file 17 is  0.000000
18.ans 18.in
error in file 18 is  0.000000
19.ans 19.in
error in file 19 is  0.000000
20.ans 20.in
error in file 20 is  0.000000
21.ans 21.in
error in file 21 is  0.000000
22.ans 22.in
error in file 22 is  0.000204
23.ans 23.in
error in file 23 is  0.000010
24.ans 24.in
error in file 24 is  0.001627
25.ans 25.in
error in file 25 is  0.000539
26.ans 26.in
error in file 26 is  0.000000
27.ans 27.in
error in file 27 is  0.000000
28.ans 28.in
error in file 28 is  0.000000
29.ans 29.in
error in file 29 is  0.000000
30.ans 30.in
error in file 30 is  0.004518
31.ans 31.in
error in file 31 is  0.001085
32.ans 32.in
error in file 32 is  0.000000
33.ans 33.in
error in file 33 is  0.000000
34.ans 34.in
error in file 34 is  0.000646
35.ans 35.in
error in file 35 is  0.000000

In [ ]: